//
//  MyReturnOfTheGoodsViewController.m
//  lmq
//
//  Created by 马田成 on 2017/3/28.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "MyReturnOfTheGoodsViewController.h"
#import "MyRefoundTableViewCell.h"
#import "Masonry.h"
#import "tuiMoneyReasonView.h"
#import "ToolAboutTime.h"
#import "UIImageView+WebCache.h"



@interface MyReturnOfTheGoodsViewController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
{
    UIView *grayView;
    NSString *tuiMoneyReason;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) tuiMoneyReasonView *tuiView;
@property (nonatomic, strong) MyRefoundTableViewCell *cell;


@end

@implementation MyReturnOfTheGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creationView];
    [self regeditCell];
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    self.navigationItem.title = @"申请退款";
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 140);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    UIButton *buttonSubmit = [[UIButton alloc] init];
    [self.view addSubview:buttonSubmit];
    [buttonSubmit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-20);
        make.height.offset(40);
        make.bottom.equalTo(self.view).with.offset(-10);
        
    }];
    buttonSubmit.backgroundColor = [UIColor blackColor];
    [buttonSubmit setTitle:@"提交申请" forState:UIControlStateNormal];
    [buttonSubmit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [buttonSubmit addTarget:self action:@selector(submitButton) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}
//提交

- (void)submitButton{
    
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    NSString *memberId = [PublicFunction getConfigWithKey:@"uid"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:@"退货退款" forKey:@"type"];
    [productParams setValue:memberId forKey:@"member_id"];
    [productParams setValue:self.order_id forKey:@"order_id"];
    [productParams setValue:self.shop_id forKey:@"shop_id"];
    [productParams setValue:tuiMoneyReason forKey:@"content"];
    [productParams setValue:self.cell.numberTextField.text forKey:@"return_quantity"];
    
    
    [[MNetworkHandler sharedInstance]conURL:My_TuiGoods
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       NSDictionary *dict1=[returnData objectForKey:@"data"];
                                       ;
                                       NSLog(@"%@", dict1);
                                       
                                       [self.navigationController popViewControllerAnimated:YES];
                                       
                                       
                                   }
                                   else {
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];


    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MyRefoundTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyRefoundTableViewCell"];
    
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 600;
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyRefoundTableViewCell" forIndexPath:indexPath];
    //无色
    self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.cell.numberTextField.delegate = self;
    self.cell.numberTextField.keyboardType= UIKeyboardTypeNumberPad;
    
    self.cell.orderSerialLabel.text = self.number;
    self.cell.orderDateLabel.text = [ToolAboutTime getTimeStrByTimeSp:self.add_time];
    self.cell.titleLabel.text = self.shop_name;
    self.cell.priceLabel.text = self.price;
    NSString *str1 = @"(";
    NSString *str2 = @")";
    //总价钱

    
    
    NSString *str3 = @"x";
    NSString *str5 = [[self.price stringByAppendingString:str3] stringByAppendingString:self.shop_num];
    
    self.cell.numPriceLabel.text =[[str1 stringByAppendingString:str5] stringByAppendingString:str2];
    
    
//    NSString *strPic = @"http://lmq.quyum.cn/";
//    
//    NSString *str = [strPic stringByAppendingString:self.shoppic];
    NSString *urlWare=self.shoppic;
    NSString *str=manage(AP_HOST, urlWare);
    NSString *strUrl = [str stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    
    [self.cell.shopPic sd_setImageWithURL:[NSURL URLWithString:strUrl]];

    
    
    [self.cell.whyLabel addTarget:self action:@selector(selectionTuiMoney) forControlEvents:UIControlEventTouchUpInside];
    
    return self.cell;
    
    
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if ([self.cell.numberTextField.text integerValue] > [self.shop_num integerValue] ) {
        
        return NO;
        
    }else {
    
        return YES;
        
    }
    

}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if ([self.cell.numberTextField.text integerValue] > [self.shop_num integerValue] ) {
        self.cell.numberTextField.text = self.shop_num;
        float a = [self.price floatValue];
        float b = [self.cell.numberTextField.text floatValue];
        float c = a*b;
        
        NSString *strPriceAll = [NSString stringWithFormat:@"%.2f", (float)c];
        self.cell.tuiMoney.text = strPriceAll;
        return NO;
        
    }else {
        
        float a = [self.price floatValue];
        float b = [self.cell.numberTextField.text floatValue];
        float c = a*b;
        
        NSString *strPriceAll = [NSString stringWithFormat:@"%.2f", (float)c];
        self.cell.tuiMoney.text = strPriceAll;
        
        
        return YES;
        
    }
    

}
- (void)selectionTuiMoney{
    
    
    grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+64)];
    grayView.backgroundColor = [UIColor blackColor];
    grayView.alpha = 0.4;
    grayView.tag = 111;
//    [[UIApplication sharedApplication].keyWindow addSubview:grayView];
    [self.view addSubview:grayView];
    
    _tuiView = [[NSBundle mainBundle] loadNibNamed:@"tuiMoneyReasonView" owner:self options:nil].lastObject;
    [self.view addSubview:self.tuiView];
    [self.tuiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
        make.top.equalTo(self.view).with.offset(150);
        make.left.equalTo(self.view).with.offset(30);
        
    }];
    self.tuiView.backgroundColor = [UIColor whiteColor];
    
    [self.tuiView.picButton addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
    [self.tuiView.button2 addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
    [self.tuiView.button3 addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
    [self.tuiView.button4 addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
    [self.tuiView.button5 addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
    [self.tuiView.button6 addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
    
    
   
    // 单击的 Recognizer
    UITapGestureRecognizer* singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];

    
    [grayView addGestureRecognizer:singleRecognizer];

}
//退款原因选择
- (void)changeTitle:(UIButton* )sender{
    if (sender.tag == 301) {
        
        self.tuiView.picButton.selected = YES;
        tuiMoneyReason = @"多拍/错拍";
        
        
        self.tuiView.button2.selected = NO;
        self.tuiView.button3.selected = NO;
        self.tuiView.button4.selected = NO;
        self.tuiView.button5.selected = NO;
        self.tuiView.button6.selected = NO;
    }else if (sender.tag == 302) {
        
        self.tuiView.picButton.selected = NO;
        self.tuiView.button2.selected = YES;
        tuiMoneyReason = @"错发/漏发";
        self.tuiView.button3.selected = NO;
        self.tuiView.button4.selected = NO;
        self.tuiView.button5.selected = NO;
        self.tuiView.button6.selected = NO;
        
    }else if (sender.tag == 303){
        
        self.tuiView.picButton.selected = NO;
        self.tuiView.button2.selected = NO;
        self.tuiView.button3.selected = YES;
        tuiMoneyReason = @"物流停滞";
        self.tuiView.button4.selected = NO;
        self.tuiView.button5.selected = NO;
        self.tuiView.button6.selected = NO;
    }else if (sender.tag == 304){
        
        self.tuiView.picButton.selected = NO;
        self.tuiView.button2.selected = NO;
        self.tuiView.button3.selected = NO;
        self.tuiView.button4.selected = YES;
        tuiMoneyReason = @"破损/丢件";
        self.tuiView.button5.selected = NO;
        self.tuiView.button6.selected = NO;
    }else if (sender.tag == 305){
        
        self.tuiView.picButton.selected = NO;
        self.tuiView.button2.selected = NO;
        self.tuiView.button3.selected = NO;
        self.tuiView.button4.selected = NO;
        self.tuiView.button5.selected = YES;
        tuiMoneyReason = @"未收到货物";
        self.tuiView.button6.selected = NO;
    }else if (sender.tag == 306){
        
        self.tuiView.picButton.selected = NO;
        self.tuiView.button2.selected = NO;
        self.tuiView.button3.selected = NO;
        self.tuiView.button4.selected = NO;
        self.tuiView.button5.selected = NO;
        self.tuiView.button6.selected = YES;
        tuiMoneyReason = @"不想要了";
    }

    
    
}
//点击隐藏view
-(void)SingleTap:(UITapGestureRecognizer*)recognizer  {
    
    [self.cell.whyLabel setTitle:tuiMoneyReason forState:UIControlStateNormal];

    self.tuiView.hidden = YES;
    grayView.hidden = YES;
    
    
    
}
//隐藏键盘  释放第一相应
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.cell.numberTextField resignFirstResponder];
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
